package ysq.xyg.sys.controller;

import org.springframework.web.multipart.MultipartFile;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.util.BaseConstant;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.sys.model.po.SysFile;
import ysq.xyg.sys.model.vo.SysFileVo;
import ysq.xyg.sys.service.SysFileService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统管理-附件管理controller层
 *
 * @author shengQiang yu
 * @date 2019-3-30 13:46:37
 */
@RestController
public class SysFileController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(SysFileController.class);
    @Resource
    private SysFileService sysFileService;

    private SysFile sysFile;

    /**
     * 分页查询
     *
     * @param sysFileVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-3-30 13:46:37
     */
    @BaseLog(module = "系统管理",className = "附件管理",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysFile/page",method = RequestMethod.POST)
    public ReturnEntity page(SysFileVo sysFileVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = sysFileService.page(sysFileVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-3-30 13:46:37
     */
    @RequestMapping(value = "/sysFile/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/sys/sysFileUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            sysFile = sysFileService.selectByPrimaryKey(id);
            view.addObject("sysFile", sysFile);
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-3-30 13:46:37
     */
    @BaseLog(module = "系统管理",className = "附件管理",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysFile/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return sysFileService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param sysFileVo Vo对象
     * @author shengQiang yu
     * @date 2019-3-30 13:46:37
     */
    @BaseLog(module = "系统管理",className = "附件管理",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysFile/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,SysFileVo sysFileVo){
        return sysFileService.addOrUpdate(sysFileVo,acceptCurrentUser(request));
    }

    /**
     * 附件上传
     *
     * @param file 文件对象
     * @param request 请求
     * @param catalog 文件保存自定义目录
     * @param objectId 文件保存objectId，空为自定义
     * @param requestFileType 文件要求类型：1、图片 ，空为不做要求
     * @param isSave 是否保存到数据库：1、是，2、否
     * @author shengQiang yu
     * @date 2019-03-30 14:44
     */
    @BaseLog(module = "系统管理",className = "附件管理",methods = "单个附件上传",businessType = BaseConstant.SYS_BUSINESS_TYPE_INSERT,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysFile/singleFile",method = RequestMethod.POST)
    public ReturnEntity singleFile(MultipartFile file, HttpServletRequest request, String catalog, String requestFileType, String objectId, String isSave){
        return sysFileService.fileUpload(file,request,catalog,requestFileType,objectId,isSave,acceptCurrentUser(request));
    }
    /**
     * 附件下载
     *
     * @param id 附件id
     * @return
     * @author shengQiang yu
     * @date 2019-03-30 14:39
     */
    @BaseLog(module = "系统管理",className = "附件管理",methods = "附件下载",businessType = BaseConstant.SYS_BUSINESS_TYPE_OTHER,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysFile/downloadById")
    public void download(HttpServletResponse response, HttpServletRequest request, String id){
        sysFileService.download(response,request,id);
    }

    /**
     * 文本编辑器图片接口
     *
     * @param file     文件对象
     * @param request  请求
     * @param catalog  文件保存自定义目录
     * @return
     * @Author: shengQiang yu
     * @Date: 2018-12-04 10:40
     */
    @BaseLog(module = "系统模块",className = "附件管理",methods = "文本编辑器图片上传",businessType = BaseConstant.SYS_BUSINESS_TYPE_OTHER,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysFile/layEditUpload",method = RequestMethod.POST)
    public ReturnEntity layEditUpload(MultipartFile file, HttpServletRequest request,String catalog){
        return sysFileService.layEditUpload(file,request,catalog);
    }
}